package com.cssl.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cssl.dao.UsersMapper;
import com.cssl.repository.UsersRepository;
import com.cssl.pojo.Users;
import com.cssl.service.UsersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Transactional
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper,Users> implements UsersService {

    @Autowired
    private UsersRepository repository;

    //=============mongodb============
    @Override
    public Users saveUsers(Users user) {
        //新增，id存在就异常
        //return repository.insert(user);
        //新增和修改，id存在就修改，否则新增
        //return repository.save(user);
        repository.saveAll(this.list());
        return user;
    }

    @Override
    public Users findByUser(Users user) {
        return repository.findByUsernameAndPassword(
                user.getUsername(),user.getPassword());
    }

    @Override
    public Page<Users> findByUsernameLike(String username,int pageIndex,int pageSize) {
        Page<Users> page = repository.findAll(PageRequest.of(
                pageIndex-1,
                pageSize,
                Sort.by(Sort.Direction.DESC,"id")));
        System.out.println("page:"+page.getTotalElements()+"\t"
                +page.getTotalPages()+"\t"+page.getNumber());
        return page;

        //页码从0开始
        /*return repository.findByUsernameLike(
                username,
                PageRequest.of(pageIndex-1,pageSize));*/
    }



    //==============redis===============
    /**
     * redis新增和查询：存在则查询redis返回，不执行方法体(不查数据库)
     * @param id
     * @return
     */
    @Cacheable(value = "users",key = "#id",cacheManager = "cacheManager1",
        condition = "#id%2==0",unless = "#result==null")
    @Override
    public Users getUserById(Integer id) {
        System.out.println("getUserById:"+id);
        return this.getById(id);
    }

    /**
     * key存在redis则修改，不存在新增
     * @param user
     * @return
     */
    @CachePut(value = "users",key = "#user.id",cacheManager = "cacheManager2")
    @Override
    public Users saveUpdate(Users user) {
        System.out.println("saveOrUpdate:"+user);
        this.saveOrUpdate(user);
        return user;
    }

    /**
     * 删除redis对应的key
     * @param id
     * @return
     */
    @CacheEvict(value = "users",key = "#id",cacheManager = "cacheManager2")
    @Override
    public boolean removeUser(int id) {
        return this.removeById(id);
    }

    /**
     * 不会以list方式保存，还是String
     * @return
     */
    @Cacheable(value = "users",cacheManager = "cacheManager2")
    @Override
    public List<Users> findAll() {
        return this.list();
    }
}
